package cn.cj.erp.common.security.security.asyncjob;

import cn.cj.erp.bo.system.SysLogBo;
import cn.cj.erp.bo.system.SysLoginLogBo;
import cn.cj.erp.common.enums.StatusEnum;
import cn.cj.erp.common.utils.LogUtils;
import cn.cj.erp.service.system.SysLogService;
import cn.cj.erp.service.system.SysLoginLogService;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.cj.erp.common.constants.HttpHeadConstants;
import cn.cj.erp.common.utils.ServletUtils;
import cn.cj.erp.common.utils.id.SnowflakeIdWorker;
import cn.cj.erp.common.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 */
@Slf4j
public class AsyncFactory {

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public static TimerTask saveLoginLog(final String username, final StatusEnum status, final String message,
                                         final Object... args) {
        final UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader(HttpHeadConstants.USER_AGENT));
        String ip = ServletUtil.getClientIP(ServletUtils.getRequest());
        return new TimerTask() {
            @Override
            public void run() {
                // 打印信息到日志
                log.info(LogUtils.getBlock(ip) +
                        LogUtils.getBlock(username) +
                        LogUtils.getBlock(status) +
                        LogUtils.getBlock(message), args);
                // 获取客户端操作系统
                String os = userAgent.getOs().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLoginLogBo sysLoginLog = new SysLoginLogBo();
                sysLoginLog.setId(SpringUtil.getBean(SnowflakeIdWorker.class).generateId());
                sysLoginLog.setUsername(username);
                sysLoginLog.setIp(ip);
                sysLoginLog.setBrowser(browser);
                sysLoginLog.setOs(os);
                sysLoginLog.setMsg(message);
                sysLoginLog.setStatus(status);
                sysLoginLog.setLoginTime(new Date());
                sysLoginLog.setCreateTime(new Date());
                sysLoginLog.setModifyTime(new Date());
                // 插入数据
                SpringUtil.getBean(SysLoginLogService.class).save(sysLoginLog);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param sysLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask SaveOperatorLog(final SysLogBo sysLog) {
        return new TimerTask() {
            @Override
            public void run() {
                sysLog.setId(SpringUtil.getBean(SnowflakeIdWorker.class).generateId());
                sysLog.setCreateTime(new Date());
                sysLog.setModifyTime(new Date());
                sysLog.setOperatorTime(new Date());
                SpringUtil.getBean(SysLogService.class).save(sysLog);
            }
        };
    }
}
